﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using QLPKWeb.BussinessDelegate.LichLamViecDelegate;
using QLPKWeb.Controllers.Base;
using QLPKWeb.Models.FormBean.LichLamViecModel;
using QLPKWeb.BussinessDelegate.CommonDelegate;

namespace QLPKWeb.Controllers
{
    public class LichLamViecController : BaseController
    {
        LichLamViecDelegate LichLamViecDelegate = new LichLamViecDelegate();
        LichLamViecSharedDelegate LichLamViecSharedDelegate = new LichLamViecSharedDelegate();
        LichLamViec_NguoiChuanBiDelegate LichLamViec_NguoiChuanBiDelegate = new LichLamViec_NguoiChuanBiDelegate();
        CommonDelegate CommonDelegate = new CommonDelegate();
        

        #region Phần Lịch làm viêc
        // danh sách lịch công viêc
        public ActionResult LichLamViec(int? id, LichLamViecForm form)
        {
            int day = (int)DateTime.Now.DayOfWeek;
            //DateTime FromDate = !string.IsNullOrEmpty(form.FromDate) ? CommonDelegate.ConvertToDateTine(form.FromDate).Value : DateTime.Today.AddDays(-day + 1);
            DateTime FromDate = !string.IsNullOrEmpty(form.FromDate) ? CommonDelegate.ConvertToDateTine(form.FromDate).Value : DateTime.Today;
            DateTime ToDate = !string.IsNullOrEmpty(form.ToDate) ? CommonDelegate.ConvertToDateTine(form.ToDate).Value : DateTime.Today.AddDays(7 - day);
            //DateTime ToDate = !string.IsNullOrEmpty(form.ToDate) ? CommonDelegate.ConvertToDateTine(form.ToDate).Value : DateTime.Today.AddDays(-day+2);
            form.ListUser = CommonDelegate.GetAllUsers().ToList();
            form.LichLamViecPagedList = LichLamViecDelegate.GetAllLichLamViecTheoEventDate_PagedList(id, FromDate, ToDate); 
            return View("~/Views/QuanLyLichLamViec/LichLamViec.aspx", form);
        }

        //vào form đăng ký lịch
        [AcceptVerbs(HttpVerbs.Get)]
        public ActionResult DangKyLich(int? id)
        {
            LichLamViecForm form = new LichLamViecForm();
            form.IsView = false;
            form.ComboboxLoai = LichLamViecDelegate.GetSelectListForComboboxLoai();
            form.ListUser = CommonDelegate.GetAllUsers().ToList();
            form.ListPhongBan = CommonDelegate.GetAllDepartments().ToList();
            form.ListUserNotInDepartment = CommonDelegate.GetAllUsersNotDepartment().ToList();

            return View("~/Views/QuanLyLichLamViec/DangKyLich.aspx", form);
        }

        [AcceptVerbs(HttpVerbs.Post)]
        [ValidateInput(false)]
        public ActionResult ThemMoiLichLamViec(LichLamViecForm form)
        {
            QLPKWeb.Models.LinQ.LichLamViec.LichLamViec newLich = LichLamViecDelegate.Insert(form);
            if (newLich.ID > 0)
                return Redirect("~/LichLamViec/LichLamViec/");
            else
            {
                form.MessageCreateLichLamViec = "Đăng ký lịch thất bại, vui lòng kiểm tra lại dữ liệu ! ";
                form.ComboboxLoai = LichLamViecDelegate.GetSelectListForComboboxLoai();
                form.ListUser = CommonDelegate.GetAllUsers().ToList();
                form.ListPhongBan = CommonDelegate.GetAllDepartments().ToList();
                form.ListUserNotInDepartment = CommonDelegate.GetAllUsersNotDepartment().ToList();
                form.IsView = false;
                return View("~/Views/QuanLyLichLamViec/DangKyLich.aspx", form);
            }
        }

        //vào form đăng ký lịch
        [AcceptVerbs(HttpVerbs.Get)]
        public ActionResult DuyetLich(int? id)
        {
            LichLamViecForm form = new LichLamViecForm();
            int day = (int)DateTime.Now.DayOfWeek;
            DateTime FromDate = !string.IsNullOrEmpty(form.FromDate) ? CommonDelegate.ConvertToDateTine(form.FromDate).Value : DateTime.Today.AddDays(-day + 1);
            DateTime ToDate = !string.IsNullOrEmpty(form.ToDate) ? CommonDelegate.ConvertToDateTine(form.ToDate).Value : DateTime.Today.AddDays(7 - day);
            form.ListUser = CommonDelegate.GetAllUsers().ToList();
            form.ListLichChuaDuyet = LichLamViecDelegate.GetAllLichLamViecTheoEventDateChuaDuyet(FromDate, ToDate, false);
            foreach (QLPKWeb.Models.LinQ.LichLamViec.LichLamViec item in form.ListLichChuaDuyet)
            {
                form.inListIdChuaDuyet += item.ID + ",";
            }
            form.ListLichDaDuyet = LichLamViecDelegate.GetAllLichLamViecTheoEventDate(FromDate, ToDate, true);
            foreach (QLPKWeb.Models.LinQ.LichLamViec.LichLamViec item in form.ListLichDaDuyet)
            {
                form.inListIdDuyet += item.ID + ",";
            }
            form.FromDate = FromDate.ToString("dd/MM/yyyy");
            form.ToDate = ToDate.ToString("dd/MM/yyyy");
            return View("~/Views/QuanLyLichLamViec/DuyetLich.aspx", form);
        }

        [AcceptVerbs(HttpVerbs.Post)]
        [ValidateInput(false)]
        public ActionResult DuyetLich(int? id, LichLamViecForm form)
        {
            int day = (int)DateTime.Now.DayOfWeek;
            DateTime FromDate = !string.IsNullOrEmpty(form.FromDate) ? CommonDelegate.ConvertToDateTine(form.FromDate).Value : DateTime.Today.AddDays(-day + 1);
            DateTime ToDate = !string.IsNullOrEmpty(form.ToDate) ? CommonDelegate.ConvertToDateTine(form.ToDate).Value : DateTime.Today.AddDays(7 - day);
            form.ListUser = CommonDelegate.GetAllUsers().ToList();
            form.ListLichChuaDuyet = LichLamViecDelegate.GetAllLichLamViecTheoEventDate(FromDate, ToDate, false);
            foreach (QLPKWeb.Models.LinQ.LichLamViec.LichLamViec item in form.ListLichChuaDuyet)
            {
                form.inListIdChuaDuyet += item.ID + ",";
            }
            form.ListLichDaDuyet = LichLamViecDelegate.GetAllLichLamViecTheoEventDate(FromDate, ToDate, true);
            foreach (QLPKWeb.Models.LinQ.LichLamViec.LichLamViec item in form.ListLichDaDuyet)
            {
                form.inListIdDuyet += item.ID + ",";
            }
            form.FromDate = FromDate.ToString("dd/MM/yyyy");
            form.ToDate = ToDate.ToString("dd/MM/yyyy");
            return View("~/Views/QuanLyLichLamViec/DuyetLich.aspx", form);
        }

        //vào form đăng ký lịch
        [AcceptVerbs(HttpVerbs.Get)]
        public ActionResult SuaLichLamViec(int id)
        {
            LichLamViecForm form = new LichLamViecForm();
            try
            {
                QLPKWeb.Models.LinQ.LichLamViec.LichLamViec obj = LichLamViecDelegate.GetLichLamViecById(id);
                form = LichLamViecDelegate.BuildLichLamViecForm(obj);
                form.ComboboxLoai = LichLamViecDelegate.GetSelectListForComboboxLoai();
                form.ListUser = CommonDelegate.GetAllUsers().ToList();
                form.ListPhongBan = CommonDelegate.GetAllDepartments().ToList();
                form.ListUserNotInDepartment = CommonDelegate.GetAllUsersNotDepartment().ToList();
                form.ListThanhPhanThamDu = LichLamViecSharedDelegate.GetAllLichLamViecShareByIDLich(id);
                form.IsView = false;
            }
            catch (Exception)
            {

            }
            return View("~/Views/QuanLyLichLamViec/DangKyLich.aspx", form);
        }

        [AcceptVerbs(HttpVerbs.Post)]
        [ValidateInput(false)]
        public ActionResult SuaLichLamViec(LichLamViecForm form)
        {
            try
            {
                LichLamViecDelegate.Update(form);
                return Redirect("~/LichLamViec/LichLamViec/");
            }
            catch (Exception)
            {
                form.ComboboxLoai = LichLamViecDelegate.GetSelectListForComboboxLoai();
                form.ListUser = CommonDelegate.GetAllUsers().ToList();
                form.ListPhongBan = CommonDelegate.GetAllDepartments().ToList();
                form.ListUserNotInDepartment = CommonDelegate.GetAllUsersNotDepartment().ToList();
                form.ListThanhPhanThamDu = LichLamViecSharedDelegate.GetAllLichLamViecShareByIDLich(form.ID);
                form.IsView = false;
                form.MessageEditLichLamViec = "Cập nhật lịch thất bại, vui lòng kiểm tra lại dữ liệu ! ";
                return View("~/Views/QuanLyLichLamViec/DangKyLich.aspx", form);
            }
        }

        [AcceptVerbs(HttpVerbs.Post)]
        [ValidateInput(false)]
        public ActionResult XoaLichLamViec(LichLamViecForm form)
        {
            try
            {
                LichLamViecDelegate.Delete(form.ID);
                return Redirect("~/LichLamViec/LichLamViec/");
            }
            catch (Exception)
            {
                form.ComboboxLoai = LichLamViecDelegate.GetSelectListForComboboxLoai();
                form.ListUser = CommonDelegate.GetAllUsers().ToList();
                form.ListPhongBan = CommonDelegate.GetAllDepartments().ToList();
                form.ListUserNotInDepartment = CommonDelegate.GetAllUsersNotDepartment().ToList();
                form.ListThanhPhanThamDu = LichLamViecSharedDelegate.GetAllLichLamViecShareByIDLich(form.ID);
                form.IsView = false;
                form.MessageEditLichLamViec = "Xóa lịch thất bại, vui lòng kiểm tra lại dữ liệu ! ";
                return View("~/Views/QuanLyLichLamViec/DangKyLich.aspx", form);
            }
        }

        [AcceptVerbs(HttpVerbs.Get)]
        public ActionResult XemLichLamViec(int id)
        {
            LichLamViecForm form = new LichLamViecForm();
            try
            {
                QLPKWeb.Models.LinQ.LichLamViec.LichLamViec obj = LichLamViecDelegate.GetLichLamViecById(id);
                form = LichLamViecDelegate.BuildLichLamViecForm(obj);
                form.ComboboxLoai = LichLamViecDelegate.GetSelectListForComboboxLoai();
                form.ListUser = CommonDelegate.GetAllUsers().ToList();
                form.ListPhongBan = CommonDelegate.GetAllDepartments().ToList();
                form.ListUserNotInDepartment = CommonDelegate.GetAllUsersNotDepartment().ToList();
                form.ListThanhPhanThamDu = LichLamViecSharedDelegate.GetAllLichLamViecShareByIDLich(id);
                form.IsView = true;
            }
            catch (Exception)
            {

            }
            return View("~/Views/QuanLyLichLamViec/DangKyLich.aspx", form);
        }

        [AcceptVerbs(HttpVerbs.Post)]
        [ValidateInput(false)]
        public ActionResult DoDuyetLich(LichLamViecForm form)
        {
            LichLamViecDelegate.DuyetLich(form.inListIdChuaDuyet, false);
            LichLamViecDelegate.DuyetLich(form.inListIdDuyet, true);
            int day = (int)DateTime.Today.DayOfWeek;
            DateTime FromDate = !string.IsNullOrEmpty(form.FromDate) ? CommonDelegate.ConvertToDateTine(form.FromDate).Value : DateTime.Today.AddDays(-day + 1);
            DateTime ToDate = !string.IsNullOrEmpty(form.ToDate) ? CommonDelegate.ConvertToDateTine(form.ToDate).Value : DateTime.Today.AddDays(7 - day);
            form.ListUser = CommonDelegate.GetAllUsers().ToList();
            form.ListLichChuaDuyet = LichLamViecDelegate.GetAllLichLamViecTheoEventDate(FromDate, ToDate, false);
            foreach (QLPKWeb.Models.LinQ.LichLamViec.LichLamViec item in form.ListLichChuaDuyet)
            {
                form.inListIdChuaDuyet += item.ID + ",";
            }
            form.ListLichDaDuyet = LichLamViecDelegate.GetAllLichLamViecTheoEventDate(FromDate, ToDate, true);
            foreach (QLPKWeb.Models.LinQ.LichLamViec.LichLamViec item in form.ListLichDaDuyet)
            {
                form.inListIdDuyet += item.ID + ",";
            }
            form.FromDate = FromDate.ToString("dd/MM/yyyy");
            form.ToDate = ToDate.ToString("dd/MM/yyyy");
            return Redirect("~/LichLamViec/DuyetLich/");
            //return View("~/Views/QuanLyLichLamViec/DuyetLich.aspx", form);
        }

        public ActionResult ViewLich(int? id, LichLamViecForm form)
        {
            DateTime Date = form.FromDate != null ? CommonDelegate.ConvertToDateTine(form.FromDate).Value : DateTime.Today;
            int day = (int)Date.DayOfWeek;
            DateTime FromDate = Date.AddDays(-day + 1);
            DateTime ToDate = Date.AddDays(7 - day);
            form.ListPhongBan = CommonDelegate.GetAllDepartments().ToList();
            form.ListUser = CommonDelegate.GetAllUsers().ToList();
            form.ListDisplayUser = CommonDelegate.GetUsersByDepartmenId("1").ToList();
            form.ListUserNotInDepartment = CommonDelegate.GetAllUsersNotDepartment().ToList();
            form.ListLichDaDuyet = LichLamViecDelegate.GetAllLichLamViecTheoEventDate(FromDate, ToDate, true);
            return View("~/Views/QuanLyLichLamViec/ViewLich.aspx", form);
        }

        public JsonResult ViewLichAjax(int? id,string StrDate)
        {
            DateTime Date = !string.IsNullOrEmpty(StrDate) ? ConVertToDateTime(StrDate) : DateTime.Today;
            int day = (int)Date.DayOfWeek;
            DateTime FromDate = Date.AddDays(-day + 1);
            DateTime ToDate = Date.AddDays(7 - day);
            List<QLPKWeb.Models.LinQ.LichLamViec.LichLamViec> ListLich = new List<Models.LinQ.LichLamViec.LichLamViec>();
            List<string[]> listData = new List<string[]>();
            if (id != null)
            {
                    if (id.Value == 0)// trưởng phòng
                    {
                    List<QLPKWeb.Models.LinQ.Common.TB_User> ListUser = CommonDelegate.GetAllUsersIsChef().Where(p => p.Department != 1).ToList();
                    listData = LichLamViecDelegate.BuildListStringListLichCongViec(ListUser, FromDate, ToDate, id.Value);
                }
                else if (id.Value == -1)// các phòng
                {
                    List<QLPKWeb.Models.LinQ.Common.TB_Department> ListDep = CommonDelegate.GetAllDepartments().Where(p => p.ID != 1).ToList();
                    for (int i = 0; i < ListDep.Count; i++)
                    {
                        string[] arr = new string[2];
                        arr[0] = ListDep[i].DepartmentName;
                        arr[1] = ListDep[i].ID.ToString();
                        listData.Add(arr);
                    }
                }
                else if (id.Value == -2)//Lãnh đạo sở
                {
                    List<QLPKWeb.Models.LinQ.Common.TB_User> ListUser = CommonDelegate.GetUsersByDepartmenId("1").ToList();
                    listData = LichLamViecDelegate.BuildListStringListLichCongViec(ListUser, FromDate, ToDate, id.Value);
                }
                else if (id.Value == -3)//toàn sở
                    return Json(LichLamViecDelegate.BuildDataLichSo(FromDate, ToDate), JsonRequestBehavior.AllowGet);
                else// chuyên viên
                {
                    List<QLPKWeb.Models.LinQ.Common.TB_User> ListUser = CommonDelegate.GetUsersByDepartmenId(id.Value.ToString()).ToList();
                    listData = LichLamViecDelegate.BuildListStringListLichCongViec(ListUser, FromDate, ToDate, id.Value);
                }
            }

            return Json(listData, JsonRequestBehavior.AllowGet);
        }

        public JsonResult ViewDetailNguoiChuanBi(int IdLich)
        {
            QLPKWeb.Models.LinQ.LichLamViec.LichLamViec objLich = LichLamViecDelegate.GetLichLamViecById(IdLich);
            string[] display = new string[objLich.LichLamViec_NguoiChuanBis.Count];
            int i = 0;
            foreach (QLPKWeb.Models.LinQ.LichLamViec.LichLamViec_NguoiChuanBi item in objLich.LichLamViec_NguoiChuanBis)
            {
                display[i] = CommonDelegate.GetDisplayNameAndDepNameByUserId(item.IdUser.ToString());
                i++;
            }
            return Json(display, JsonRequestBehavior.AllowGet);
        }

        public JsonResult ViewDetailThanhPhan(int IdLich)
        {
            QLPKWeb.Models.LinQ.LichLamViec.LichLamViec objLich = LichLamViecDelegate.GetLichLamViecById(IdLich);
            string[] display = new string[objLich.LichLamViecShareds.Count];
            int i = 0;
            foreach (QLPKWeb.Models.LinQ.LichLamViec.LichLamViecShared item in objLich.LichLamViecShareds)
            {
                display[i] = CommonDelegate.GetDisplayNameAndDepNameByUserId(item.IdUser.ToString());
                i++;
            }
            return Json(display, JsonRequestBehavior.AllowGet);
        }

        private DateTime ConVertToDateTime(string StrDate)
        {
            string[] arr = StrDate.Split('/');
            string date = arr[2] + "-" + arr[1] + "-" + arr[0];
            return DateTime.Parse(date);
        }

        #endregion
    }
}
